package letcode34

func NextGreatestLetter(letters []byte, target byte) byte {
	res := findPos(letters, byte(int(target)+1)) - 1
	if res >= len(letters) || res < 2 {
		return letters[0]
	}
	return letters[res]

}

func findPos(letters []byte, target byte) int {
	left, right := -1, len(letters)
	for left+1 < right {
		mid := (left + right) >> 1
		if letters[mid] < target {
			left = mid
		} else {
			right = mid
		}
	}
	return right
}
